package utils

import (
	"bytes"
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/base64"
	"encoding/pem"
	"errors"
	"fmt"
	"math/big"
)

var signStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

const SharePublicKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxyWrUY1MOX2gPzT1Av4CVmhjb
gwRa9RLuEj5qG4W0yHJMqKjHzY3GLS+F8L46ln7Q/YufoXBYzJfjDFtahByX55Ug
T51J2WQRBfNFIJb+iE8mEtb6D2TKISmQ0dEShoqY7JBrDiYtSVyYbW/wOSZrl21V
r43N565RsQLnmOSJYwIDAQAB
-----END PUBLIC KEY-----`

const SharePrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCxyWrUY1MOX2gPzT1Av4CVmhjbgwRa9RLuEj5qG4W0yHJMqKjH
zY3GLS+F8L46ln7Q/YufoXBYzJfjDFtahByX55UgT51J2WQRBfNFIJb+iE8mEtb6
D2TKISmQ0dEShoqY7JBrDiYtSVyYbW/wOSZrl21Vr43N565RsQLnmOSJYwIDAQAB
AoGAMrkum44DAlTCEdASq8h3XTdr5CULNxA46GeYGxnjGeMeiTSNgwPocSQvLN20
yG5rnCDalx6uOQeWRhcecULEDTCNxHXNRAyITtlpYoyXRLMGE1vAaJ+cL1nqfzC1
Gj0eb9bnOrXRQ6cLUBGhQmsnLzYQoWUYm1zMvaHF8RAcqSECQQDsMN9sX7Di0E63
X+X6mu5e53Ccdam9AA4Q/FfKEEwDIK3EYWmQ4Z69VChVd5yPoCrfHZEB5L6+LIuB
1wk7LoqLAkEAwLKUyzrVe0S2wVxAQtkv+fDA6STSCHMK5XY8rt11n0Cq0R6i5T1q
zvhgXPS9MGqlaoUP7fRo7jHdpOUgvaLPiQJBALJPz00dDQifJN5R2ukQcGdbqOul
9QhH81ale42EMi6mTCmXNUB45QOgdnbXv227CMK6qo/YZpxiOsxCUKjKYTMCQQCK
TKE16xfkzMBNrqQAVlh0jjImImML5WVgoqeS6SbOEV+HjvFijd3mhaTDknNmGGR4
Ot/f7JaSvf9RpZEGCZ/hAkAFuhu1lIuzYzn0wGxFOu70hEB5qdz4gdUnH6yjwM38
cYwKE+zg1ns1ty8FGMsrA5Exm2QGG3pyscM4mspuvzjM
-----END RSA PRIVATE KEY-----`

const DownloadPublicKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC6iPUq2QmeMLtAeaqpa5YvOGgD
cf0uEEESz0Evx8iRT8igb2buQl5ThQg4jOvveHRAKAcew36Q1PeWNvnW++ItFJcK
ByVfz8wen1IAtrvIz8v58dRlRohGpow+woBDH3TmbjaFrjLGJ82+Y5QITAx9E8sx
gliZNo3rH/4U4LmdrQIDAQAB
-----END PUBLIC KEY-----`

const DownloadPrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC6iPUq2QmeMLtAeaqpa5YvOGgDcf0uEEESz0Evx8iRT8igb2bu
Ql5ThQg4jOvveHRAKAcew36Q1PeWNvnW++ItFJcKByVfz8wen1IAtrvIz8v58dRl
RohGpow+woBDH3TmbjaFrjLGJ82+Y5QITAx9E8sxgliZNo3rH/4U4LmdrQIDAQAB
AoGAWuDI2SQ+XFSPlfzwmXPyCnVnr24ElEGVAo34y0eBPtJpA0jjE+c/v18Lv0lg
1R2RPYSC24kRC4zGRufn1saGzOy3BZIqiCqbNjcndDwpxlO7/ef53qZ2NKDzqkKJ
bYgEbVfANu+bChr99n16jeAN/Fsa888XRBvT4dhYfvTHhCECQQDhiQHHozQa6y6h
dAt/CkP63yHZdBImbJr1U3dDDApKyxrMYXQQQy+YQq6ipOCnQNqlEhajdb0uqxqi
TvCM9bjFAkEA07tTkNDpKGVcV1UI89CTa4ddp+NzXL9u7JdW4urL6drH/6j3KOBZ
P5jnhl0n9aXgHNW8t6eO7lVX2G9NRZgPyQJAelp7jrSpddkU6R4thnQpQRaAJJpl
plbREjp/vu3vP3hER5CvCIuKbI3wc+gCwHncpCTWB0O+qGzG/DrbIX3FCQJBAI+D
CEg/IdpMocNcEt5XwKSAsx1p0LbL1p6skarq5N4Ueb0zRJvDPRsbQ8rR95TMItTI
lsqQ2xN2az17a4/BWukCQQC2OnarIZljPVlxGRLcN/MoKltgyGUD4ibhz1SyDsPd
VH84niK/F2kuBtGklrcbrROt2NDrVl4UvRqGkk4XeC5J
-----END RSA PRIVATE KEY-----`

func CreateRandomString(len int) string {
	var container string
	b := bytes.NewBufferString(signStr)
	length := b.Len()
	bigInt := big.NewInt(int64(length))
	for i := 0; i < len; i++ {
		randomInt, _ := rand.Int(rand.Reader, bigInt)
		container += string(signStr[randomInt.Int64()])
	}
	return container
}

func RsaEncodeByPublic(encodeString []byte, sharePublicKey string) (string, error) {
	result, err := RsaEncrypt(encodeString, []byte(sharePublicKey))
	return string(result), err
}

func RsaEncrypt(plaintext []byte, pub_key []byte) ([]byte, error) {

	block, _ := pem.Decode(pub_key)
	if block == nil {
		return nil, errors.New("invalid rsa public key")
	}

	pubInf, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		return nil, err
	}
	pub := pubInf.(*rsa.PublicKey)
	return rsa.EncryptPKCS1v15(rand.Reader, pub, plaintext)
}

func RsaDecodeByPrivateKey(decodeString []byte, privateKey string) (string, error) {
	result, err := RsaDecrypt(decodeString, []byte(privateKey))
	return string(result), err
}

func RsaDecrypt(ciphertext []byte, pri_key []byte) ([]byte, error) {

	block, _ := pem.Decode(pri_key)
	if block == nil {
		return nil, errors.New("invalid rsa private key")
	}

	private, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		return nil, err
	}

	return rsa.DecryptPKCS1v15(rand.Reader, private, ciphertext)
}

func CheckPassword(inputPassword string, dbPassword string) bool {

	//passwordEncryption, err := utils.RsaEncodeByPublic([]byte("qbs1"), DownloadPublicKey)
	//if err != nil {
	//	fmt.Println(err)
	//}

	//inputPassword = base64.StdEncoding.EncodeToString([]byte(passwordEncryption))

	inputPasswordBase64Decode, err := base64.StdEncoding.DecodeString(inputPassword)
	if err != nil {
		return false
	}
	inputPass, err := RsaDecodeByPrivateKey(inputPasswordBase64Decode, DownloadPrivateKey)
	if err != nil {
		return false
	}
	fmt.Println(inputPass)

	dbPasswordBase64Decode, err := base64.StdEncoding.DecodeString(dbPassword)
	if err != nil {
		return false
	}
	dbPass, err := RsaDecodeByPrivateKey(dbPasswordBase64Decode, SharePrivateKey)
	if err != nil {
		return false
	}

	fmt.Println(dbPass)
	return inputPass == dbPass
}
